﻿using DAL;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Model;

namespace WebApi.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class FangXingController : ControllerBase
    {
        public FangXingController(FangXing_DAL Dal,IWebHostEnvironment env)
        {
            this.Dal = Dal;
            Env = env;
        }

        public FangXing_DAL Dal { get; }
        public IWebHostEnvironment Env { get; }

        /// <summary>
        /// 房型添加
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        [HttpPost("FXAdd")]
        public  IActionResult FXAdd(FangXing_Model m)
        {
            var m1=Dal.FXAdd(m);
            return Ok(new
            {
                data = m1
            }); 
        }
        /// <summary>
        /// 上传图片
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        [HttpPost("TP")]
        public IActionResult TP(IFormFile file)
        {
            try
            {
                var root = Env.ContentRootPath;
                var path = root + "/img/" + file.FileName;
                using (FileStream fs = System.IO.File.Create(path))
                {
                    file.CopyTo(fs);
                    fs.Close();
                }
                return Ok(new
                {
                    data = "https://localhost:7135" + "/img/" + file.FileName
                });
            }
            catch (Exception)
            {
                return Ok(new
                {
                    data ="图片上传失败"
                });
            }
           
        }
    }
}
